/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月18日         luwei
 */

package com.coocaa.app.gamecenter.pages.list;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.coocaa.app.gamecenter.framework.GameCenterActivity;
import com.coocaa.app.gamecenter.framework.ui.LoadMoreGridView.LoadMoreDataCallback;
import com.coocaa.app.gamecenter.framework.ui.LoadMoreGridView.OnGridItemClickListener;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.logsdk.appstore.LogAPIForAppStore;
import com.skyworth.webSDK.webservice.tcappstore.AppStoreBean;
import com.skyworth.webSDK.webservice.tcappstore.AppStoreBeanList;
import com.skyworth.webSDK.webservice.tcappstore.AppStoreException;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyListAppData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GameListActivity extends GameCenterActivity implements LoadMoreDataCallback,
        OnGridItemClickListener {
    private GameCenterListLayout mLayout = null;
    public static final String CATEGORY_ID = "id";
    public static final String CATEGORY_NAME = "name";
    private String mCategoryId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub133
        Uri uri = getIntent().getData();
        if (uri != null){
            mCategoryId = uri.getQueryParameter(CATEGORY_ID);
        }else{
            mCategoryId = getIntent().getStringExtra(CATEGORY_ID);
        }
        if (mCategoryId == null || mCategoryId.equals("")) {
            finish();
        }
        LogUtils.d("uri", "the category id is " + mCategoryId);
        super.onCreate(savedInstanceState);
        showloading();
    }

    @Override
    protected Map<String, String> getPageStartParams() {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("game_list_id",mCategoryId);
        return map;
    }

    @Override
    protected View createContentView() {
        mLayout = new GameCenterListLayout(this);
        mLayout.setLoadMoreCallback(this);
        mLayout.hideFocusView();
        mLayout.loadData();
        return mLayout;
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void updateContentView() {
        Uri uri = getIntent().getData();
        if (uri != null){
            LogUtils.d("uri","the category name is " + uri.getQueryParameter(CATEGORY_NAME));
            updateTitle(uri.getQueryParameter(CATEGORY_NAME));
        }else{
            updateTitle(getIntent().getStringExtra(CATEGORY_NAME));
        }

    }

    @Override
    protected void onDestroy() {
        mLayout.destory();
        super.onDestroy();
    }

    @Override
    public List<SkyListAppData> loadMordData(int page) {
        try {
            if (page == 1) {
                startSlowNetTimerDalay(7000);
            }
            AppStoreBeanList beanList = CoocaaApplication.getCoocaaWebService()
                    .getAppStoreService().getGamesByClassId(Integer.valueOf(mCategoryId), page, 15);
            cancelSlowNetTimer();
            if (beanList != null && beanList.data != null && beanList.data.size() > 0) {
                ArrayList<SkyListAppData> baseDatas = new ArrayList<SkyListAppData>();
                for (AppStoreBean bean : beanList.data) {
                    SkyListAppData data = new SkyListAppData(bean.id, bean.name, bean.lmg,
                            getString(R.string.downloaded_count) + bean.downloads, bean.packagename);
                    data.setControll(bean.extra);
                    baseDatas.add(data);
                }
                mLayout.setTotalNum(Integer.valueOf(beanList.count));
                hideloading();
                mLayout.showFocusView();
                return baseDatas;
            } else {
                hideloading();
                if (page == 1) {
                    setLoadDataErrorMsg(getString(R.string.no_app_list));
                }
            }

        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (page == 1) {
                setLoadDataErrorMsg(getString(R.string.no_app_list));
            }
        } catch (AppStoreException e) {
            LogAPIForAppStore.reportInterfaceError("获取对应分类的游戏列表", Android.throwable2String(e));
            e.printStackTrace();
            if (page == 1) {
                setLoadDataErrorMsg(getString(R.string.no_app_list));
            }
        }
        hideloading();
        return new ArrayList<SkyListAppData>();
    }

    @Override
    public void onClick(int position, Object baseData) {

    }

    @Override
    public String getName() {
        return "酷游吧列表页";
    }

}
